package com.example.huawei.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.huawei.system.entity.CommunityRelease;
import com.example.huawei.system.entity.LikeRecord;
import com.example.huawei.system.entity.SysUser;
import com.example.huawei.system.mapper.CommunityReleaseMapper;
import com.example.huawei.system.service.CommunityReleaseService;
import com.example.huawei.system.service.LikeRecordService;
import com.example.huawei.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 15097789205
 * @description 针对表【community_release】的数据库操作Service实现
 * @createDate 2025-06-27 09:48:14
 */
@Service
public class CommunityReleaseServiceImpl extends ServiceImpl<CommunityReleaseMapper, CommunityRelease>
        implements CommunityReleaseService {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private LikeRecordService likeRecordService;

    @Override
    public IPage<CommunityRelease> queryPage(Page<CommunityRelease> page, CommunityRelease communityRelease) {
        IPage<CommunityRelease> result = baseMapper.queryPage(page, communityRelease);
        List<CommunityRelease> records = result.getRecords();

        //用户map
        Map<String, SysUser> userMap = sysUserService.list().stream().collect(Collectors.toMap(SysUser::getId, user -> user, (k1, k2) -> k1));

        long now = new Date().getTime();
        for (CommunityRelease record : records) {
            SysUser sysUser = userMap.get(record.getUserId());
            record.setAvatar(sysUser.getAvatar());
            record.setUsername(sysUser.getUsername());
            record.setJoinTime(((now - sysUser.getCreateTime().getTime()) / (24 * 60 * 60 * 1000)) + "天");
            record.setLikeFlag(likeRecordService.count(
                    new LambdaQueryWrapper<LikeRecord>()
                            .eq(LikeRecord::getUserId, communityRelease.getUserId())
                            .eq(LikeRecord::getReleaseId, record.getId())
                            .eq(LikeRecord::getLikeType, 1)
                            .eq(LikeRecord::getLikeFlag, 1)) > 0
            );
        }
        return result;
    }

    @Override
    public CommunityRelease queryById(CommunityRelease communityRelease) {

        CommunityRelease entity = baseMapper.queryById(communityRelease.getId());
        SysUser sysUser = sysUserService.lambdaQuery().eq(SysUser::getId, communityRelease.getUserId()).one();
        long now = new Date().getTime();
        entity.setAvatar(sysUser.getAvatar());
        entity.setUsername(sysUser.getUsername());
        entity.setJoinTime("" + ((now - sysUser.getCreateTime().getTime()) / (24 * 60 * 60 * 1000)) + "天");
        entity.setLikeFlag(likeRecordService.lambdaQuery().eq(LikeRecord::getUserId, communityRelease.getUserId())
                .eq(LikeRecord::getReleaseId, entity.getId())
                .eq(LikeRecord::getLikeType, 1)
                .eq(LikeRecord::getLikeFlag, 1).count() > 0
        );
        return entity;
    }
}




